
"""
1,生成一个文件夹，文件夹下面生成100个txt文件，分别命名为1.txt ,2.txt到100.txt,
    其中1.txt内容为1到100,2.txt的内容为101到200，
    以此类推到100.txt的内容为9901到10000。

2，输出如下格式的日志信息
"""

import os
import logging
import glob
class Practice0809:

    def __init__(self  , *args):
        self.filePath = args[0]
        self.total_count = args[1]
        self.content_count = args[2]


    def create_folder(self):
        if not(os.path.exists(self.filePath)):
            os.mkdir(self.filePath)  # 默认相对路径
            print(f"路径：{self.filePath}创建成功")
        else:
            print(f"路径：{self.filePath}已经存在")
        #pass

    def create_file_list(self):
        #创建多个文件列表  10000 // 100 = 100
        # 1 = 1-100
        # 2 = 101-200
        count = self.total_count // self.content_count
        for tmp in range(1 , count + 1):
            begin = (tmp - 1) * self.content_count + 1
            end = tmp * self.content_count
            self.create_file(str(tmp)+'.txt' ,  begin , end)


    def create_file(self , file_name , begin , end):
        tmp_file_name = self.filePath +'/' +file_name
        #创建单个文件
        # result =  [num for num in result if num is not None]
        content_list = [str(tmp) for tmp in range(begin , end + 1)]
        content_str = ','.join(content_list)
        with open(tmp_file_name , mode='w' , encoding='utf8') as f:
            f.writelines(content_str)

    def execute(self):
        #先创建文件夹
        self.create_folder()
        #再创建文件列表
        self.create_file_list()

    def print_log(self):
        logging.debug('logging debug message')
        logging.info('logging info message')
        logging.warning('logging warning message')
        logging.error('logging error message')
        logging.critical('logging critical message')


    def create_file_list2(self):
        try:
            os.mkdir("day10_tst")
        except FileExistsError:
            for filename in glob.glob("day10_tst/*.txt"):
                os.remove(filename)
            print("文件夹已存在，删除文件夹下的所有文件")
        except Exception:
            print("未知异常，文件夹创建失败已")
        num = 1
        for i in range(1, 101):
            with open(f"day10_tst/{i}.txt","w", encoding="utf-8") as f:
                for n in range(num, num + 100):
                    f.write(f"{n}\n")
            num += 100


    def create_file_list3(self):
        if not os.path.exists("D:/day10"):
            os.makedirs('D:/day10')
        for i in range(1, 101):
            file_path = f'D:/day10/{i}.txt'
            with open(file_path,'w')as f:
                start=(i-1)*100+1
                end = i * 100
                for j in range(start, end + 1):
                    f.write(str(j) + '\n')

    def create_file_list4(self):
        # 薇化代妈
        os.makedirs('D:/day10/v002', exist_ok=True)
        for i in range(1, 101):
            file_path = f'D:/day10/v002/{i}.txt'
            with open(file_path,'w') as f:
                content ='\n'.join(str(j) for j in range((i-1)*100+1,i*100+1))
                #f.write(content)


#设定日志输出格式
#logging.basicConfig(level=logging.INFO, format='%(name)s---%(levelname)s: %(message)s')
#logging.basicConfig(level=logging.INFO, format='%(asctime)s ---%(name)s --- %(levelname)s: %(message)s',datefmt='%Y-%m-%d %H:%M:%S')
# logging.basicConfig(level=logging.DEBUG, format='%(asctime)s --- %(levelname)s: %(message)s',datefmt='%Y/%m/%d %H:%M:%S')
#输出到日志文件
logging.basicConfig(filename='my_log.log' ,level=logging.DEBUG, format='%(asctime)s --- %(levelname)s: %(message)s',datefmt='%Y/%m/%d %H:%M:%S')
#logging.info("111")
obj = Practice0809('test' , 10000 , 100)
#obj.execute()
obj.print_log()
obj.create_file_list2()